package com.myteam.fugaocrm.sqldb.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.myteam.fugaocrm.modle.FgPartnerCompanyDetail;

public interface FgPartnerCompanyDetailMapper {
	
	@Select("SELECT partner_detail_id,partner_company_id, partner_product_id, partner_contact_id,"
			+ "(SELECT cm.partner_company_name FROM fg_partner_company cm WHERE  cm.partner_company_id = d.partner_company_id AND cm.is_delete = 0) partnerCompanyName,"
			+ "project_id,is_shipment,is_billing,is_payment,shipment_date,billing_type,billing_date,payment_date,created_user,modified_user,created_date,modified_date,is_delete "
			+ "from fg_partner_company_detail  d WHERE is_delete = 0 and project_id = #{projectId}")
	public List<FgPartnerCompanyDetail> selectByProjectId(Integer projectId);

	@Insert("INSERT into fg_partner_company_detail( partner_company_id, partner_product_id, partner_contact_id, project_id, is_shipment, is_billing, is_payment, shipment_date, billing_type, billing_date, payment_date, created_user, modified_user, created_date, modified_date, is_delete) "
			+ "VALUES(#{partnerCompanyId},#{partnerProductId},#{partnerContactId},#{projectId},#{isShipment},#{isBilling},#{isPayment},#{shipmentDate},#{billingType},#{billingDate},#{paymentDate},#{createdUser},#{modifiedUser},#{createdDate},#{modifiedDate},#{isDelete});")
	public void insertPartnerCompanyDetail(FgPartnerCompanyDetail partnerDetail);
	
	public void updateOne(FgPartnerCompanyDetail partnerDetail);
	
	@Update("update fg_partner_company_detail set is_delete = 1 where partner_detail_id = #{partnerDetailId}")
	public void deleteOne(Integer partnerDetailId);
	
	//合作商付款提醒查询
	@Select(
		"SELECT " +
			"detail.*, " +
			"project.project_name " +
		"FROM fg_partner_company_detail detail " +
		"LEFT JOIN fg_project project ON project.project_id = detail.project_id " +
		"WHERE detail.is_delete = 0 AND detail.is_payment = 'N' AND payment_date IS NOT NULL AND to_days(payment_date) < to_days(now())"
	)
	public List<FgPartnerCompanyDetail> selectAllWaitPay();
}
